package com.helloworld.demo.threadbase;

public class ThreadInterrupt2 implements Runnable {

    @Override
    public void run() {
        int count = 0;
        while (!Thread.currentThread().isInterrupted() && count < 1000){
            System.out.println("count = " + count++);

            try {
                Thread.sleep(1); //sleep 会重置可打断状态为false
                System.out.println(Thread.currentThread().isInterrupted()+"...........................");
                //Thread.currentThread().interrupt();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }

        }
        System.out.println("线程停止运行");
    }
}


class Test{
    public static void main(String[] args) {
        ThreadInterrupt2 threadInterrupt2 = new ThreadInterrupt2();
        Thread thread = new Thread(threadInterrupt2);
        thread.start();
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
    }
}
